package com.jdy.userservice.config.shiro;



import com.jdy.userservice.entity.SysUser;
import com.jdy.userservice.service.SysUserService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;


@Component
public class CustomRealm extends AuthorizingRealm {

    @Resource
    @Lazy
    private SysUserService userService;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        //获取登录用户名
        String name = (String) principalCollection.getPrimaryPrincipal();
        //查询用户名称
        //添加角色和权限
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();

        simpleAuthorizationInfo.addRole("admin");

        return simpleAuthorizationInfo;
    }

    /**
     * @MethodName doGetAuthenticationInfo
     * @Description 认证配置类
     * @Param [authenticationToken]
     * @Return AuthenticationInfo
     * @Author WangShiLin
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        if (StringUtils.isEmpty(authenticationToken.getPrincipal())) {
            return null;
        }
        System.out.println("————身份认证方法————");
        String username = (String) authenticationToken.getPrincipal();
//        // 解密获得username，用于和数据库进行对比
//        String username = JWTUtil.getUsername(token);
//        if (username == null || !JWTUtil.verify(token, username)) {
//            throw new AuthenticationException("token认证失败！");
//        }
        SysUser password = userService.getPassword(username);
        if (password == null) {
            throw new AuthenticationException("该用户不存在！");
        }
        //这里验证authenticationToken和simpleAuthenticationInfo的信息
        SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(username, password.getPassword(), getName());
        return simpleAuthenticationInfo;
    }


    /**
     * 必须重写此方法，不然会报错
     */
//    @Override
//    public boolean supports(AuthenticationToken token) {
//        return token instanceof JWTToken;
//    }
//
//    /**
//     * 默认使用此方法进行用户名正确与否验证，错误抛出异常即可。
//     */
//    @Override
//    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
//        System.out.println("————身份认证方法————");
//        String token = (String) authenticationToken.getCredentials();
////        // 解密获得username，用于和数据库进行对比
////        String username = JWTUtil.getUsername(token);
//
////        if (username == null || !JWTUtil.verify(token, username)) {
////            throw new AuthenticationException("token认证失败！");
////        }
//        /* 以下数据库查询可根据实际情况，可以不必再次查询，这里我两次查询会很耗资源
//         * 我这里增加两次查询是因为考虑到数据库管理员可能自行更改数据库中的用户信息
//         */
////        String password = userService.getPassword(username);
////        if (password == null) {
////            throw new AuthenticationException("该用户不存在！");
////        }
////        int ban = userService.checkUserBanStatus(username);
////        if (ban == 1) {
////            throw new AuthenticationException("该用户已被封号！");
////        }
//        SysUser sysUser = new SysUser();
//        sysUser.setUsername("admin");
//        sysUser.setPassword("admin");
//        SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(sysUser,"admin", "admin");
//        return  simpleAuthenticationInfo;
//    }
//
//    /**
//     * 只有当需要检测用户权限的时候才会调用此方法，例如checkRole,checkPermission之类的
//     */
//    @Override
//    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
//        System.out.println("————权限认证————");
////        String username = JWTUtil.getUsername(principals.toString());
//        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
////        //获得该用户角色
////        List<SysRole> role = userService.getRole(username);
////        //每个角色拥有默认的权限
//////        List<SysRolePermission> rolePermission = userService.getRolePermission(username);
////        //每个用户可以设置新的权限
////        List<SysPermission>  permission = userService.getPermission(username);
////        Set<String> roleSet = new HashSet<>();
////        Set<String> permissionSet = new HashSet<>();
////        //需要将 role, permission 封装到 Set 作为 info.setRoles(), info.setStringPermissions() 的参数
////        for (int i = 0; i <role.size() ; i++) {
////            roleSet.add(role.get(i).getRoleCode());
////        }
////        for (int i = 0; i <permission.size() ; i++) {
////            permissionSet.add(permission.get(i).getId());
////        }
////        for (int i = 0; i <rolePermission.size() ; i++) {
////            permissionSet.add(rolePermission.get(i).getPermissionId());
////        }
//        //设置该用户拥有的角色和权限
////        info.setRoles(roleSet);
////        info.setStringPermissions(permissionSet);
//        return info;
//    }
}